package com.dayaokeji.rhythmschoolstudent.boxing.impl;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.ImageView;


import com.bilibili.boxing.loader.IBoxingCallback;
import com.bilibili.boxing.loader.IBoxingMediaLoader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.dayaokeji.rhythmschoolstudent.R;


/**
 * Created by Gorden on 2017/11/7.
 */

public class BoxingGlideLoader implements IBoxingMediaLoader {

    private static RequestOptions configRequestOptions() {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.mipmap.ic_boxing_default_image);
        requestOptions.error(R.mipmap.ic_boxing_default_image);
        requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
        requestOptions.centerCrop();
        return requestOptions;
    }

    @Override
    public void displayThumbnail(@NonNull ImageView imageView, @NonNull String absPath, int width, int height) {
        String path = "file://" + absPath;
        try {
            RequestManager requestManager = Glide.with(imageView.getContext());
            configRequestOptions().override(width,height);
            requestManager.setDefaultRequestOptions(configRequestOptions());
            requestManager.load(path).into(imageView);
        } catch(IllegalArgumentException ignore) {
        }
    }

    @Override
    public void displayRaw(@NonNull final ImageView imageView, @NonNull String absPath, int width, int height, final IBoxingCallback iBoxingCallback) {
        String path = "file://" + absPath;
        RequestManager requestManager = Glide.with(imageView.getContext());
        if (width > 0 && height > 0) {
            configRequestOptions().override(width,height);
            requestManager.setDefaultRequestOptions(configRequestOptions());
        }
        RequestBuilder<Bitmap> request = requestManager.asBitmap().load(path);
        request.listener(new RequestListener<Bitmap>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                if (iBoxingCallback != null) {
                    iBoxingCallback.onFail(e);
                    return true;
                }
                return false;
            }

            @Override
            public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                if (resource != null && iBoxingCallback != null) {
                    imageView.setImageBitmap(resource);
                    iBoxingCallback.onSuccess();
                    return true;
                }
                return false;
            }
        }).into(imageView);
    }
}
